package com.tim.source;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import com.tim.source.common.TableInfo;
import com.tim.source.common.TableToHbmXml;
import com.tim.source.templete.SpringBeanProc;
import com.tim.source.templete.TableToDaoImpl;
import com.tim.source.templete.TableToEntity;
import com.tim.source.templete.TableToIDao;
import com.tim.source.templete.TableToIService;
import com.tim.source.templete.TableToServiceImpl;
import com.tim.source.util.StringUtil;

public class MakeSource {
	public static String DB_TYPE = null;
	// TODO 项目路径
	public static final String PROJECT = "F:/workspace/shdwhy/";
	// TODO 生成文件保存包
	public static final String CLASS = "com/kim/cyjoy/wap/";
	// TODO 生成Entity包
	public static final String ENTITY = CLASS + "entity/";
	// TODO 生成Service包
	public static final String SERVICE = CLASS + "service/";
	// TODO 生成ServiceImpl包
	public static final String SERVICE_IMPL = SERVICE + "impl/";
	// TODO  生成Dao包
	public static final String DAO = CLASS + "dao/";
	// TODO 生成DaoImpl包
	public static final String DAO_IMPL = DAO + "impl/";
	// TODO 生成hbm.xml保存路径
	public static final String HBM_XML = PROJECT + "WebContent/WEB-INF/mappings/";
	// TODO 生成SpringBean.xml配置文件路径
	public static final String SPRING_BEAN = PROJECT + "resource/conf/beans-service.xml";

	public static void main(String[] args) {
		InputStream stream = MakeSource.class
				.getResourceAsStream("sql.properties");
		Properties pp = new Properties();
		try {
			pp.load(stream);
			// 获取数据库类型
			DB_TYPE = pp.getProperty(BaseConst.DB_TYPE);
			// 获取数据库表-列查询Sql
			String sql = pp.getProperty(BaseConst.QUERY + "." + DB_TYPE);
			// 获取数据库-主键查询Sql
			String idSql = pp.getProperty(BaseConst.ID + "." + DB_TYPE);
			// 获取数据库表
			String tables = pp.getProperty(BaseConst.TABLES);
			String[] tableArr = tables.split(",");
			StringBuilder springBean = new StringBuilder();
			for (int i = 0; i < tableArr.length; i++) {
				TableInfo.tableName = tableArr[i];
				// 获取主键列信息
				TableToHbmXml.setIdColName(idSql);
				// 主键非空-表存在
				if (!"".equals(TableInfo.idColName)) {
					// 数据表转hbm.xml
					String hbmString = TableToHbmXml.execute(sql);
					StringUtil.convertToFile(hbmString, HBM_XML + TableInfo.className + ".hbm.xml");
					// 生成IService
					String iserviceString = TableToIService.execute();
					StringUtil.convertToFile(iserviceString, PROJECT + "src/" + SERVICE + TableToIService.CLASS + ".java");
					// 生成ServiceImpl
					String serviceString = TableToServiceImpl.execute();
					StringUtil.convertToFile(serviceString, PROJECT + "src/" + SERVICE_IMPL + TableToServiceImpl.CLASS + ".java");
					// 生成IDao
					String idaoString = TableToIDao.execute();
					StringUtil.convertToFile(idaoString, PROJECT + "src/" + DAO + TableToIDao.CLASS + ".java");
					// 生成DaoImpl
					String daoString = TableToDaoImpl.execute();
					StringUtil.convertToFile(daoString, PROJECT + "src/" + DAO_IMPL + TableToDaoImpl.CLASS + ".java");
					// 生成Entity
					String entityString = TableToEntity.execute();
					StringUtil.convertToFile(entityString, PROJECT + "src/" + ENTITY + TableInfo.className + ".java");
					// 连接spring配置文件
					String tempStr = SpringBeanProc.execute();
					if (tempStr != null) {
						springBean.append(tempStr);
					}
					System.out.println("对表《" + TableInfo.tableName + "》生成代码成功!");
				} else {
					System.out.println("表《" + TableInfo.tableName + "》不存在!");
				}
			}
			String beanString = SpringBeanProc.getBeanString(springBean.toString());
			StringUtil.convertToFile(beanString, SPRING_BEAN);
			System.out.println("生成application-service.xml成功");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
